package com.example.hw_demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.hw_demo.entity.User;
import com.example.hw_demo.service.UserService;
import com.example.hw_demo.mapper.UserMapper;
import com.example.hw_demo.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
* @author Administrator
* @description 针对表【user】的数据库操作Service实现
* @createDate 2025-06-19 08:58:32
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    UserMapper userMapper;
    @Override
    public Result selectPage(User user) {
        //分页
        //1、创建分页对象
        Page<User> page = new Page<User>(user.getPage(),user.getSize());
        //2、查询条件
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        if(user.getUserName()!=null&&!"".equals(user.getUserName())){
            queryWrapper.like("user_name",user.getUserName());
        }
        if(user.getUserAccount()!=null&&!"".equals(user.getUserAccount())){
            queryWrapper.like("user_account",user.getUserAccount());
        }

        //3、分页查询
        userMapper.selectPage(page,queryWrapper);
        List<User> list = page.getRecords();//获取查询数据
        long total = page.getTotal();//总行数
        HashMap<String,Object> hashMap = new HashMap<String,Object>();
        hashMap.put("list",list);
        hashMap.put("total",total);
        return new Result(200,"查询成功",hashMap);
    }
}




